package com.wolfpire.news.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.wolfpire.news.service.NewService;
import com.wolfpire.profession.service.ProfessionService;
import com.wolfpire.system.common.AuthorityUtils;
import com.wolfpire.system.common.EasyuiPage;
import com.wolfpire.system.common.EasyuiPagination;
import com.wolfpire.system.common.Page;
import com.wolfpire.system.common.Result;
import com.wolfpire.system.common.springmvc.PrincipalArgument;
import com.wolfpire.system.model.Authority;
import com.wolfpire.system.model.DataDict;
import com.wolfpire.system.model.New;
import com.wolfpire.system.model.Org;
import com.wolfpire.system.model.Profession;
import com.wolfpire.system.model.Role;
import com.wolfpire.system.model.User;
import com.wolfpire.system.service.RoleService;

/**
 * @author xp
 *
 */

@Controller
@RequestMapping("/news")
public class NewsController {
	
	@Resource private ProfessionService professionService;
	@Resource private NewService newService;
	@RequestMapping("/list")
	public ModelAndView list(@PrincipalArgument User user, @RequestParam Long menuId) {
		//获取对应菜单的操作权限
//		List<Authority> operAuthorities = getAperAuthorities(user, menuId);
		List<Authority> operAuthorities = AuthorityUtils.getAperAuthorities(user, menuId);
		return new ModelAndView("newsManage/new_list", "operAuthorities", operAuthorities);
	}
	
	@ResponseBody
	@RequestMapping("/queryList")
	public EasyuiPagination<New> queryList(EasyuiPage<New> page, New filterNew) {
		Page<New> news = this.newService.setPageDataList(page, filterNew);
		return new EasyuiPagination<New>(news);
	}
	
	@ResponseBody
	@RequestMapping("/professions")
	public List<Profession> professions(Profession profession) {
		/*if (null != profession && null != profession.getId()) {
			org.setParentId(org.getId());
			org.setId(null);
		}*/
		List<Profession> professions = this.professionService.list(profession);
		return professions;
	}
	
	@RequestMapping(value = "/add")
	public ModelAndView add(HttpSession session) {
		ModelAndView mv = new ModelAndView("newsManage/new_add");
		return mv;
	}
	
	@RequestMapping("/edit")
	public ModelAndView edit(Long id) {
		ModelAndView mv = new ModelAndView("newsManage/new_edit");
		if (null != id) {
			New newInfo = this.newService.get(id);
			//Role role = this.roleService.getAuthorityInfoRole(id); 
					//this.roleService.get(id);
			mv.getModelMap().put("newInfo", newInfo);
		}
		return mv;
	}
	
	@ResponseBody
	@RequestMapping("/save")
	public Map<String, Object> save(New newNew) {
		Map<String, Object> map = new HashMap<String, Object>();
		boolean result = false;
		String msg = null;
		try {
			this.newService.saveOrUpdate(newNew);
			result = true;
			msg = "保存成功";
		} catch (Exception e) {
			e.printStackTrace();
			msg = "保存失败，请联系管理员";
		}
		map.put("result", result);
		map.put("msg", msg);
		return map;
	}
	
	@ResponseBody
	@RequestMapping("/delete")
	public Result delete(Long id) {
		try {
			this.newService.delete(id);
			return new Result(true, "删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "删除失败");
		}
	}
}
